package com.lanhe.article.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanhe.article.domain.LhArticle;
import com.lanhe.article.domain.LhArticleContent;
import com.lanhe.article.mapper.LhArticleMapper;
import com.lanhe.article.service.ILhArticleContentService;
import com.lanhe.article.service.ILhArticleService;
import com.lanhe.common.constant.Constants;
import com.lanhe.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 文章表(LhArticle)表服务实现类
 *
 * @author zhanggl
 * @since 2023-12-18 09:23:43
 */
@Service
public class LhArticleServiceImpl extends ServiceImpl<LhArticleMapper, LhArticle> implements ILhArticleService {

	@Autowired
	private LhArticleMapper articleMapper;

	@Autowired
	private ILhArticleContentService contentService;

	/**
	 * 文章列表
	 *
	 * @param article 文章实体
	 * @return java.util.List<com.lanhe.article.domain.LhArticle>
	 * @author zhanggl
	 * @date 2023/12/18
	 */
	@Override
	public List<LhArticle> selectArticleList(LhArticle article) {
		LambdaQueryWrapper<LhArticle> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.like(StringUtils.isNotBlank(article.getTitle()), LhArticle::getTitle, article.getTitle());
		queryWrapper.eq(ObjectUtil.isNotNull(article.getpType()), LhArticle::getpType, article.getpType());
		queryWrapper.eq(ObjectUtil.isNotNull(article.getType()), LhArticle::getType, article.getType());
		queryWrapper.ge(ObjectUtil.isNotNull(article.getBeginTime()), LhArticle::getCreateTime, article.getBeginTime());
		queryWrapper.le(ObjectUtil.isNotNull(article.getEndTime()), LhArticle::getCreateTime, article.getEndTime());
		queryWrapper.eq(LhArticle::getDelFlag, 0);
		queryWrapper.eq(ObjectUtil.isNotNull(article.getType()),LhArticle::getDelFlag, 0);
		queryWrapper.orderByDesc(LhArticle::getArticleId);
		return articleMapper.selectList(queryWrapper);
	}

	/**
	 * 查询文章信息
	 *
	 * @param articleId 文章ID
	 * @return com.lanhe.article.domain.LhArticle
	 * @author zhanggl
	 * @date 2023/12/18
	 */
	@Override
	public LhArticle selectArticleById(Long articleId) {
		LambdaQueryWrapper<LhArticle> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhArticle::getDelFlag, 0)
				.eq(LhArticle::getArticleId, articleId);
		LhArticle lhArticle = articleMapper.selectOne(queryWrapper);
		LhArticleContent articleContent = contentService.selectArticleContentByArticleId(articleId);
		if (ObjectUtil.isNotNull(articleContent)){
			lhArticle.setContent(articleContent.getContent());
		}

		// 更新访问量
		LhArticle article = new LhArticle();
		article.setPv(ObjectUtil.isNull(lhArticle.getPv()) ? 1 :  lhArticle.getPv()+ 1);
		articleMapper.update(article,queryWrapper);
		return lhArticle;
	}

	/**
	 * 新增文章
	 *
	 * @param article 文章实体
	 * @return 结果
	 * @author zhanggl
	 * @date 2023/12/18
	 */
	@Override
	@Transactional
	public int insertArticle(LhArticle article) {
		int insertNum = articleMapper.insert(article);
		contentService.insertArticleContent(article.getArticleId(),article.getContent());
		return insertNum;
	}

	/**
	 * 修改文章
	 *
	 * @param article 文章实体
	 * @return int
	 * @author zhanggl
	 * @date 2023/12/18
	 */
	@Override
	public int updateArticle(LhArticle article) {
		LambdaQueryWrapper<LhArticle> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhArticle::getDelFlag, 0)
				.eq(LhArticle::getArticleId, article.getArticleId());
		int updateNum = articleMapper.update(article, queryWrapper);
		contentService.updateArticleContent(article.getArticleId(),article.getContent());
		return updateNum;
	}

	/**
	 * 删除文章
	 *
	 * @param articleIds 文章ID
	 * @return int
	 * @author zhanggl
	 * @date 2023/12/18
	 */
	@Override
	public int deleteArticleByIds(Long[] articleIds) {
		LambdaUpdateWrapper<LhArticle> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.set(LhArticle::getDelFlag, Constants.GeneralIntType.TWO+"")
				.in(LhArticle::getArticleId,articleIds);
		return articleMapper.update(null,updateWrapper);

	}
}

